package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.dataObject.User;
import com.nchu.student_score.mapper.TeachTaskMapper;
import com.nchu.student_score.mapper.TeacherMapper;
import com.nchu.student_score.model.Teacher;
import com.nchu.student_score.service.TeacherService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Objects;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    TeachTaskMapper taskMapper;

    @Override
    public ResultVo<Teacher> getTeachers(Integer academyId) {
        ResultVo<Teacher> resultVo = new ResultVo<>();
        Teacher teacher = new Teacher();
        teacher.setAcademyId(academyId);
        resultVo.setList(teacherMapper.findTeacher(teacher));
        return resultVo;
    }

    @Override
    public ResultVo<Teacher> updateTeacher(Teacher input, Integer oldTeacherId) {
        ResultVo<Teacher> resultVo = new ResultVo<>();

        if(oldTeacherId == -1){//新增教师
            Teacher teacher = teacherMapper.selectByPrimaryKey(input.getTeacherId());
            if(teacher != null){
                resultVo.setStatus(1);
                resultVo.setMessage("目标教师编号已存在!");
                return resultVo;
            }

            input.setTeacherPassword(this.getInitPwd(input));
            teacherMapper.insert(input);
            resultVo.setStatus(0);
            resultVo.setMessage("新增教师 " + input.getTeacherId() + " " + input.getTeacherName() + " 成功!");
        }
        else{//修改教师
            Teacher oldTeacher = teacherMapper.selectByPrimaryKey(oldTeacherId);
            if(oldTeacher == null){
                resultVo.setStatus(1);
                resultVo.setMessage("原教师编号未找到!");
                return resultVo;
            }

            if(! Objects.equals(input.getTeacherId(), oldTeacherId)){//需要修改教师编号
                Teacher teacher = teacherMapper.selectByPrimaryKey(input.getTeacherId());
                if(teacher != null){
                    resultVo.setStatus(2);
                    resultVo.setMessage("目标教师编号已存在!");
                    return resultVo;
                }
                //修改教学安排中的教师id
                User user = new User();
                user.setId(String.valueOf(oldTeacherId));
                user.setNewId(String.valueOf(input.getTeacherId()));
                taskMapper.updateTeacherId(user);
                //修改教师
                input.setTeacherPassword(oldTeacher.getTeacherPassword());
                teacherMapper.deleteByPrimaryKey(oldTeacherId);
                teacherMapper.insert(input);
            }
            else{//无需修改教师编号
                input.setTeacherPassword(oldTeacher.getTeacherPassword());
                teacherMapper.updateByPrimaryKey(input);
            }
            resultVo.setStatus(0);
            resultVo.setMessage("修改教师成功!");
        }

        return resultVo;
    }

    @Override
    public PageInfo<Teacher> getTeacherList(Teacher teacher, Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(teacherMapper.findTeacher(teacher));
    }

    @Override
    public ResultVo<Teacher> initTeacherPwd(Teacher teacher) {
        ResultVo<Teacher> resultVo = new ResultVo<>();
        teacher.setTeacherPassword(this.getInitPwd(teacher));

        teacherMapper.updateByPrimaryKey(teacher);
        resultVo.setStatus(0);
        resultVo.setMessage("教师”" + teacher.getTeacherId() + " - "
                + teacher.getTeacherName() + "”的密码重置成功!");

        return resultVo;
    }

    private String getInitPwd(Teacher teacher){//获得初始密码
        String idCard = teacher.getTeacherIdCard();
        String newPwd = "" + teacher.getTeacherId() + idCard.substring(12,18);//密码为id+身份证号后6位
        return DigestUtils.md5DigestAsHex(newPwd.getBytes());
    }
}
